import numpy as np
import pandas as pd
import copy,os
import scipy.io as sio
from Network import LSTMForSmooth,LSTMForClassify
from sklearn.preprocessing import MinMaxScaler
import warnings
warnings.filterwarnings("ignore")
def loaddata(filename):
    load_plane = os.path.join(os.getcwd(),"./dataset/"+filename+'.mat')  #mat文件路径
    plane = sio.loadmat(load_plane) #使用scipy读入mat文件数据
    data1=np.array(plane['X%s_DE_time'%(filename)])[0:800]
    data2=np.array(plane['X%s_FE_time'%(filename)])[0:800]
    data3=np.array(plane['X%s_BA_time'%(filename)])[0:800]
    data=np.column_stack((data1,data2,data3))
    return data

def polynomial_mapping(data):
    map_data=copy.deepcopy(data)
    for i in range(data.shape[1]):
        map_data = np.column_stack((map_data, data[:, i:i + 1]**2))
    for i in range(data.shape[1]-1):
        for j in range(i+1,data.shape[1]):
            map_data=np.column_stack((map_data,data[:,i:i+1]*data[:,j:j+1]))
    map_data=map_data[9:]
    return map_data

def smooth_data(data):
    Strides=10
    scaler1= MinMaxScaler()
    Normalized_data = scaler1.fit_transform(data)
    dataX=[];dataY=[]
    for i in range(0,Normalized_data.shape[0]-Strides):
        dataX.append(Normalized_data[i:i+Strides-1])
        dataY.append(Normalized_data[i+Strides])
    dataX=np.array(dataX)
    dataY=np.array(dataY)
    Smooth_dataY=LSTMForSmooth(dataX,dataY)
    Smooth_dataY=scaler1.inverse_transform(Smooth_dataY)
    return Smooth_dataY


def experiential_mapping(data):
    rows,columns=data.shape
    for i in range(columns):
        print(f"*******{i}********")
        if(i==0):
            smoothed_data=smooth_data(data[:,i:i+1])
        else:
            smoothed_data=np.column_stack((smoothed_data,smooth_data(data[:,i:i+1])))
    new_matrix = []
    K=10#K表示K步差分计算
    for j in range(smoothed_data.shape[0]-K):
        new_matrix.append(smoothed_data[j+K]-smoothed_data[j])
    new_matrix=np.column_stack((data[20:],new_matrix))
    return new_matrix


def Mapping_method(method,data):
    if(method=="经验映射"):
        Mappeddata=experiential_mapping(data)
    elif(method=="多项式核显示映射"):
        Mappeddata = polynomial_mapping(data)
    return Mappeddata

def main(method,filename,diagnostic_model):
    dataX=[];dataY=[]
    step_size=5##滑动窗口的步长
    window_size=10##滑动窗口的长度
    for i in range(len(filename)):
        Rawdata = loaddata(filename[i])  # 原始数据
        Mappeddata=Mapping_method(method,Rawdata)
        ####对特征扩增后的数据进行归一化处理
        scaler1 = MinMaxScaler()
        Normalized_Mappeddata = scaler1.fit_transform(Mappeddata)
        for start in range(0, Normalized_Mappeddata.shape[0] - window_size + 1, step_size):
            # 切片操作获取当前窗口的数据
            window = Normalized_Mappeddata[start:start + window_size]
            dataX.append(window)
            dataY.append(i)
    dataX=np.array(dataX)
    dataY=np.array(dataY)
    ##选择故障诊断的模型
    if(diagnostic_model=="LSTM"):
        LSTMForClassify(dataX,dataY)
    else:
        pass


if __name__ == '__main__':
    method="多项式核显示映射"#提供两种映射方法，一种是“经验映射”，另外一种是“多项式核显示映射”
    filename = ["105", "118", "130"]#"105", "118", "130"分别代表一种轴承故障，详情可访问https://www.cnblogs.com/gshang/p/10712809.html
    diagnostic_model="LSTM"##采用LSTM作为故障诊断模型"
    main(method, filename, diagnostic_model)









